package com.neusoft.nep.SystemAdminstrator.Controller;

import com.neusoft.nep.GridOfficer.entity.Officer;
import com.neusoft.nep.SystemAdminstrator.Common.FileUtills;
import com.neusoft.nep.SystemAdminstrator.entity.SystemFeedback;
import com.neusoft.nep.entity.Feedback;
import com.neusoft.nep.entity.GirdFeedback;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AQImakesureController {

    @FXML
    private TableColumn<GirdFeedback,Integer> colIndex;
    @FXML
    private TableColumn<GirdFeedback,String> colProv;
    @FXML
    private TableColumn<GirdFeedback,String> colCity;
    @FXML
    private TableColumn<GirdFeedback,String> colAQIlevel;
    @FXML
    private TableColumn<GirdFeedback,String> colData;
    @FXML
    private TableColumn<GirdFeedback,String> colTime;
    @FXML
    private TableColumn<GirdFeedback,String> colGridName;
    @FXML
    private TableColumn<GirdFeedback,String> colFeedbackName;
    @FXML
    private TableColumn<GirdFeedback,Void> colDetail;
    @FXML
    private TableView<GirdFeedback> tableView;
    private ObservableList<GirdFeedback> tablelist = FXCollections.observableArrayList();
    @FXML
    private ComboBox comProv,comCity;
    @FXML
    private DatePicker datePicker;
    @FXML
    private Button btn_clear;
    @FXML
    private Button btn_query;


    Map<String, List<String>> provinceMap = new HashMap<>();

    public void Clear(ActionEvent event){
        comProv.getSelectionModel().clearSelection();
        comCity.getSelectionModel().clearSelection();
        comProv.requestLayout();
        datePicker.setValue(LocalDate.now());
        comProv.setPromptText("请输入");
        comCity.setPromptText("请输入");
    }

    public void query(){
        // 获取用户选择的过滤条件
        String selectedProvince = (String) comProv.getSelectionModel().getSelectedItem();
        String selectedCity = (String) comCity.getSelectionModel().getSelectedItem();
        LocalDate selectedDate = datePicker.getValue();

        // 获取所有数据
        List<String> list = FileUtills.readListObject("AssignofficerNames.txt");
        ObservableList<GirdFeedback> allData = FXCollections.observableArrayList();
        for (String string : list) {
            Map<String, GirdFeedback> girdFeedbackMap = FileUtills.readMapObject("gird" + string + "back.txt");
            for (GirdFeedback girdFeedback : girdFeedbackMap.values()) {
                girdFeedback.setGridName(string);
                allData.add(girdFeedback);
            }
        }

        // 根据过滤条件筛选数据
        ObservableList<GirdFeedback> filteredData = FXCollections.observableArrayList();
        for (GirdFeedback feedback : allData) {
            boolean provinceMatch = selectedProvince == null || feedback.getProvince().equals(selectedProvince);
            boolean cityMatch = selectedCity == null || feedback.getCity().equals(selectedCity);
            boolean dateMatch = selectedDate == null || feedback.getDate().equals(selectedDate);

            if (provinceMatch && cityMatch && dateMatch) {
                filteredData.add(feedback);
            }
        }

        // 更新表格中的数据
        tableView.setItems(filteredData);

    }

    public void initialize(){
        labID.setText("系统管理员：" + loginController.currentLoginID);
        datePicker.setValue(LocalDate.now());
        //设置GridFeedback的可视化属性
        colIndex.setCellValueFactory(new PropertyValueFactory<>("index"));
        colProv.setCellValueFactory(new PropertyValueFactory<>("province"));
        colAQIlevel.setCellValueFactory(new PropertyValueFactory<>("aqiLevel"));
        colCity.setCellValueFactory(new PropertyValueFactory<>("city"));
        colData.setCellValueFactory(new PropertyValueFactory<>("date"));
        colTime.setCellValueFactory(new PropertyValueFactory<>("timePart"));
        colGridName.setCellValueFactory(new PropertyValueFactory<>("GridName"));
        colFeedbackName.setCellValueFactory(new PropertyValueFactory<>("name"));

        List<String> list = FileUtills.readListObject("AssignofficerNames.txt");
        for ( String string : list){
//            System.out.println(string);
            Map<String,GirdFeedback> girdFeedbackMap = FileUtills.readMapObject("gird" + string + "back.txt");
            for (GirdFeedback girdFeedback : girdFeedbackMap.values()){
                girdFeedback.setGridName(string);
                tablelist.add(girdFeedback);
            }
        }
        tableView.setItems(tablelist);
        colDetail.setCellFactory(cols->new TableCell<>(){
            private Button btn = new Button("详情");//创建一个按钮
            {//设置onAction
                btn.setOnAction(e -> {
                    //获取当前行的那个Girdfeedback对象
                    GirdFeedback girdFeedback = getTableView().getItems().get(getIndex());
                    FXMLLoader fxmlLoader = new FXMLLoader();
                    URL url = getClass().getResource("../View/AQImakesureDetali.fxml");
                    fxmlLoader.setLocation(url);
                    Parent root = null;
                    try {
                        root = fxmlLoader.load();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    //要记住，不管是什么控制器，本质上它还是一个java类，是类就能创建实例然后调用方法
                    AQImakesureDetailController aqImakesureDetailController = fxmlLoader.getController();
                    aqImakesureDetailController.setGirdFeedback(girdFeedback);
                    Scene scene = btn.getScene();
                    scene.setRoot(root);
                    Stage stage = (Stage) scene.getWindow();
                    stage.show();
                });
            }
            //用来实时更新并显示按钮
            @Override
            protected void updateItem(Void item,boolean emtiy){
                super.updateItem(item,emtiy);
                setGraphic(emtiy?null:btn);
            }

        });
        provinceMap.put("辽宁省",List.of("沈阳市","大连市"));
        provinceMap.put("吉林省",List.of("长春市","吉林市"));
        provinceMap.put("黑龙江省",List.of("哈尔滨市","齐齐哈尔市"));

        comProv.setItems(FXCollections.observableArrayList(provinceMap.keySet()));
        comProv.getSelectionModel().clearSelection();
        comProv.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue< ? extends String> observableValue , String oldValue , String newValue) {
                List<String> citys = provinceMap.getOrDefault(newValue,List.of());
                comCity.setItems(FXCollections.observableArrayList(citys));
                comCity.getSelectionModel().clearSelection();
            }
        });

    }



    @FXML
    private Button btn_out;
    @FXML
    private Button btn_back;
    public void toback(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/index.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
    public void OUT(ActionEvent event){
        System.exit(0);
    }
    @FXML
    private MenuItem mi_PSL;
    public void  PSLClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/publicList.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
    @FXML
    private MenuItem mi_AQI;
    public void  AQIListClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AQImakesure.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
    @FXML
    private MenuItem mi_PGC;
    public void PGCClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/ProvincialGroupComponentItemInspectionStatistics.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
    @FXML
    private MenuItem mi_AqiDistribute;
    public void AqiDistributeClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AqiDistributeTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
    @FXML
    private Label labID;
    @FXML
    private MenuItem mi_Other;
    public void OtherClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/OtherTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
    @FXML
    private MenuItem mi_AqiTrendTotal;
    public void AqiTrendClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AqiTrendTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }





}
